/**************************************************************************
 *
 * Copyright 2014 MaeckySoft Ltd., Bludenz, Austria.
 * All Rights Reserved.
 *
 **************************************************************************/

#include <MWindow.h>
#include <MFTManager.h>
#include <MPNG.h>
#include <MTools.h>
#include <MConfigManager.h>
#include <MFontTexture.h>
#include <iostream>
#include <string>
#include <sstream>

/* define some default values ...
*/
#define _MDEBUG MDBG_INFO
#define _CFGFILE "m3d.cfg"
#define _FTSize 72
#define _FTPathU "fonts/courier.ttf"
#define _FTPathW "fonts\courier.ttf"
/* end of defaults
*/

#ifndef T
#define T( text ) ((char*)text)
#endif

int main(int argc, char *argv[])
{

    std::string sFilename="";
    std::string sFontname="";
    std::string sCharset="";
    int iFontsize;
    int iDebug;

    const int iWinWidth = 800, iWinHeight = 800;

    bool bWindows=false;

    if (argc == 2)
    {
        sFilename.append(argv[1]);
    }
    else
    {
        cout << std::endl << "usage: " << argv[0] << " [filename]" << std::endl << std::endl;
        return 0;
    }

    /* Read Config file from _CFGFile
    */
	MConfigManager *mcm = new MConfigManager(_MDEBUG);
    mcm->setFileName(T(_CFGFILE));
    mcm->loadConfig();
    std::string sTmp;

    sTmp = mcm->getValue(T("font_file"));
    if (sTmp == "")
    {
#ifdef __WIN32
        sFontname = _FTPathW;
#else
        sFontname = _FTPathU;
#endif
    }
    else
    {
        sFontname = sTmp;
    }

    sTmp = mcm->getValue(T("font_size"));
    if ( !(istringstream (sTmp) >> iFontsize))
        iFontsize = _FTSize;

    sTmp = mcm->getValue(T("debug_mode"));
    if ( !(istringstream (sTmp) >> iDebug))
        iDebug = _MDEBUG;

    sTmp = mcm->getValue(T("char_set"));
    if (sTmp =="")
    {
        sCharset = sTmp;
    }

    delete(mcm);
    /* end read config File
    */

    if (bWindows)
    {
#ifdef __unix__
        MWindow *MW_MainWindow = new MWindow(iDebug);
        MW_MainWindow->createWindow(iWinWidth,iWinHeight, 0);
        if (MW_MainWindow!=NULL) delete(MW_MainWindow);
#endif
    }

    if (!bWindows)
    {

        MFontTexture *mft = new MFontTexture(iDebug);
        mft->setFont(sFontname);
		mft->setOutputFileName(sFilename);
        mft->setFontSize(iFontsize);
        mft->setRenderCharSet(sCharset);
        mft->renderCharSet();

        if (sCharset == "") sCharset = mft->getRenderCharSet();

        std::cout << std::endl;
        std::cout << "fontname        : " << sFontname << std::endl;
        std::cout << "render charset  : " << sCharset << std::endl;
        std::cout << "rendersize      : " << iFontsize << " point" << std::endl;
		std::cout << "max font width  : " << mft->getMaxGlyphWidth() << " pixel" << std::endl;
		std::cout << "max font height : " << mft->getMaxGlyphHeight() << " pixel" << std::endl;
		std::cout << "image width     : " << mft->calculateImageWidth() << " pixel" << std::endl;
		std::cout << "image height    : " << mft->calculateImageHeight() << " pixel" << std::endl;
        std::cout << "output files    : " << sFilename << ".png"  << std::endl;
        std::cout << "                  " << sFilename << ".gly" << std::endl << std::endl;

		mft->packCharSet();

        delete(mft);



    }

    return 0;
}

